package com.wyz.saas.common.dialog

import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.util.TypedValue
import com.wyz.saas.common.R

class DialogService {
    private val ctx: Context

    constructor(ctx: Context) {
        this.ctx = ctx
    }



    fun showDialog(
        title: String,
        message: String,
        one: DialogConfig.One?
    ) {
        showDialog(title , message , null , one)
    }

    fun showDialog(
        title: String,
        message: String,
        builder: DialogConfig.Custom?,
        one: DialogConfig.One?
    ) {
        val view = builder?.view(ctx)?.also {
            val tv = TypedValue()
            var padding = 0
            if (ctx.theme.resolveAttribute(R.attr.dialogPreferredPadding, tv, true)) {
                padding = TypedValue.complexToDimensionPixelSize(
                    tv.data,
                    ctx.resources.displayMetrics
                )
            }
            it.setPadding(padding, padding, padding, 0)
        }

        val dlgAlert = AlertDialog.Builder(ctx)
        dlgAlert.setMessage(message)
        dlgAlert.setTitle(title)
        view?.also {
            dlgAlert.setView(it)
        }
        one?.also {
            dlgAlert.setPositiveButton(ctx.getString(one.positive)) { dialog, which ->
                builder?.onPos()?.onClick(dialog, which)
                it.onPositive(dialog)
            }
            dlgAlert.setCancelable(it.backupCancel)
        }
        dlgAlert.create().apply {
            setDialog(this , builder , one)
        }.show()
    }

    fun showDialog(
        title: String,
        message: String,
        two: DialogConfig.Two?
    ) {
        showDialog(title , message , null , two)
    }

    fun showDialog(
        title: String,
        message: String,
        builder: DialogConfig.Custom?,
        two: DialogConfig.Two?
    ) {
        val view = builder?.view(ctx)?.also {
            val tv = TypedValue()
            var padding = 0
            if (ctx.theme.resolveAttribute(R.attr.dialogPreferredPadding, tv, true)) {
                padding = TypedValue.complexToDimensionPixelSize(
                    tv.data,
                    ctx.resources.displayMetrics
                )
            }
            it.setPadding(padding, padding, padding, 0)
        }

        val dlgAlert = AlertDialog.Builder(ctx)
        dlgAlert.setMessage(message)
        dlgAlert.setTitle(title)
        view?.also {
            dlgAlert.setView(it)
        }
        two?.also {
            dlgAlert.setPositiveButton(ctx.getString(two.positive)) { dialog, which ->
                builder?.onPos()?.onClick(dialog, which)
                it.onPositive(
                    dialog
                )
            }
            dlgAlert.setNegativeButton(ctx.getString(two.negative)) { dialog, which ->
                builder?.onNeg()?.onClick(dialog, which)
                it.onNegative(
                    dialog
                )
            }
            dlgAlert.setCancelable(it.backupCancel)
        }
        dlgAlert.create().apply {
            setDialog(this , builder , two)
        }.show()
    }

    fun showDialog(
        title: String,
        message: String,
        three: DialogConfig.Three?
    ) {
        showDialog(title , message , null , three)
    }
    fun showDialog(
        title: String,
        message: String,
        builder: DialogConfig.Custom?,
        three: DialogConfig.Three?
    ) {
        val view = builder?.view(ctx)?.also {
            val tv = TypedValue()
            var padding = 0
            if (ctx.theme.resolveAttribute(R.attr.dialogPreferredPadding, tv, true)) {
                padding = TypedValue.complexToDimensionPixelSize(
                    tv.data,
                    ctx.resources.displayMetrics
                )
            }
            it.setPadding(padding, padding, padding, 0)
        }

        val dlgAlert = AlertDialog.Builder(ctx)
        dlgAlert.setMessage(message)
        dlgAlert.setTitle(title)
        view?.also {
            dlgAlert.setView(it)
        }
        three?.also {
            dlgAlert.setPositiveButton(ctx.getString(three.positive)) { dialog, which ->
                builder?.onPos()?.onClick(dialog, which)
                it.onPositive(
                    dialog
                )
            }
            dlgAlert.setNegativeButton(ctx.getString(three.negative)) { dialog, which ->
                builder?.onNeg()?.onClick(dialog, which)
                it.onNegative(
                    dialog
                )
            }
            dlgAlert.setNeutralButton(ctx.getString(three.neutral)) { dialog, which ->
                builder?.onNeu()?.onClick(dialog, which)
                it.onNeutral(
                    dialog
                )
            }
            dlgAlert.setCancelable(it.backupCancel)
        }
        dlgAlert.create().apply {
            setDialog(this , builder , three)
        }.show()
    }


    private fun setDialog(dialog : Dialog, builder: DialogConfig.Custom?, config: DialogConfig.One?){
        builder?.onShow(dialog)?.also {
            dialog.setOnShowListener(it)
        }
        builder?.onCancel()?.also {
            dialog.setOnCancelListener(it)
        }
        builder?.onKey()?.also {
            dialog.setOnKeyListener(it)
        }
        builder?.onDismiss()?.also {
            dialog.setOnDismissListener(it)
        }
        config?.also {
            dialog.setCanceledOnTouchOutside(it.touchOutSideCancel)
        }
    }

}